package com.yc.utils;


import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 *
 * @author UNO
 * 2021年6月25日下午8:45:22
 */
public class StringUtil {

	/**
	 * 检查字符串是否为空的方法 为空返回true  ， 不为空返回false
	 * @param strs
	 * @return
	 */
	public static boolean checkNull(String...strs) {
		if(strs == null || strs.length <=0) {
			return true;
		}
		for(String str : strs) {
			if(str == null || "".equals(str)) {
				return true;				
			}
		}
		return false;
	}

	//检查字符串是否为纯数字的方法  是整数返回true,否则返回false 
	public static boolean isInteger(String str) {  
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
		return pattern.matcher(str).matches();  
	}

	//检查邮箱格式是否正确的方法  正确返回true 错误返回false
	public static boolean checkEmail(String str) {
		boolean flag = false;
		String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern p;
		Matcher m;
		p = Pattern.compile(regEx1);
		m = p.matcher(str);
		if(m.matches()) {
			flag = true;
		}else {
			System.out.println("输入邮箱格式错误......");
		}
		return flag;

	}
	/**
	 * 获得当前时间的字符串格式
	 * @return
	 */
	public static String getTimes() {
		//创建Calendar对象
		Calendar cal=Calendar.getInstance();
		//用Calendar类提供的方法获取年、月、日、时、分、秒
		int year  =cal.get(Calendar.YEAR);   //年
		int  month =cal.get(Calendar.MONTH)+1;  //月  默认是从0开始  即1月获取到的是0
		int day   =cal.get(Calendar.DAY_OF_MONTH);  //日，即一个月中的第几天
		int hour  =cal.get(Calendar.HOUR_OF_DAY);  //小时
		int minute=cal.get(Calendar.MINUTE);   //分
		//拼接成字符串输出
		String date1 = year+"/"+month+"/"+day+" "+hour+":"+minute;
		String date2 = year+"/0"+month+"/0"+day+" "+hour+":"+minute;
		String date3 = year+"/0"+month+"/"+day+" "+hour+":"+minute;
		String date4 = year+"/"+month+"/0"+day+" "+hour+":"+minute;
		
		if(month < 10 && day < 10) {
			return date2;
		}else if(month < 10 && day >= 10) {
			return date3;  
		}else if(month >= 10 && day < 10) {
			return date4;
		}else {
			return date1;
		}
	}
	/**
	 * 判断输入的是否为汉字
	 * @param string
	 * @return
	 */
	 public static boolean isChinese(char c) {
	     Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
	     if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
	         || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
	         || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
	         || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
	         || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
	         || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
	       return true;
	     }
	     return false;
	   }
	 public static boolean checkNameChese(String name) {
	     boolean res = true;
	     char[] cTemp = name.toCharArray();//转换为数组
	     for (int i = 0; i < name.length(); i++) {
	       if (!isChinese(cTemp[i])) {//逐个判断是否为中文
	         res = false;
	         break;
	       }
	     }
	     return res;
	   }
}
